﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using secondhand.L2busslogic;
using secondhand.L1database;
using secondhand.L3object;
using System.IO;

namespace secondhand.L4view
{
    public partial class saler_fabu : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            bool has_no_file = false;
            if (TextBox1.Text.Length == 0)
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('商品名称不能为空 ');</script>");
                return;
            }
            if (TextBox2.Text.Length == 0)
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('商品类型不能为空 ');</script>");
                return;
            }
            if (TextBox3.Text.Length == 0)
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('商品价格不能为空 ');</script>");
                return;
            }
            if (TextBox4.Text.Length == 0)
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('商品描述不能为空 ');</script>");
                return;
            }
            if (!FileUpload1.HasFile)
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('您还没有上传商品图片，请上传！ ');</script>");
                return;
            }
            string photoName = Path.GetFileName(FileUpload1.PostedFile.FileName);
            int i = photoName.LastIndexOf("."); //取得文件名中最后一个"."的索引 
            string newext = photoName.Substring(i); //获取文件扩展名
            if (newext != ".gif" && newext != ".jpg" && newext != ".jpeg" && newext != ".bmp" && newext != ".png" && newext !=".JPG")
            {
                Response.Write("文件格式不正确!");
                Response.End();
            }
            string fileType = FileUpload1.FileName.Substring(FileUpload1.FileName.LastIndexOf("."));
            string path = Server.MapPath("~/") + "\\L4View\\image\\" + photoName;
            FileUpload1.SaveAs(path);
            has_no_file = FileUpload1.HasFile;
            goodsOB mydd = new goodsOB();
            mydd.goods_name = TextBox1.Text;
            mydd.goods_type = TextBox2.Text;
            mydd.goods_price = TextBox3.Text;
            mydd.goods_describe = TextBox4.Text;
            mydd.saler_name = Session["salerName"].ToString();
            mydd.master_check = "未审核";
            mydd.sale_time = DateTime.Now.ToString(); 
            if (!has_no_file)
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('请上传商品图片 ');</script>");
                return;
            }
            mydd.goods_picture = "~/L4View/image/" + photoName;
            if (goodsBL.Addfill(mydd) == true)
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('发布成功 ');</script>");
                return;
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('发布不成功 ');</script>");
                return;
            }
        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}